﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LIMS.BLL;
using LIMS.Model;


namespace LIMS.Web
{
    public partial class AdmLabOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindLabIDs();
                bindTeacher();
                AspNetPager1.RecordCount = LabOrderManager.GetAllOrder().Count;
                rpOrderLabManager.DataSource = LabOrderManager.GetAllOrder();
                rpOrderLabManager.DataBind();
            }
        }

        private void bindTeacher()
        {
            selTeacher.DataSource = TeacherManager.GetAllTeacher();
            selTeacher.DataTextField = "teacherName";
            selTeacher.DataValueField = "teacherID";
            selTeacher.DataBind();
            selTeacher2.DataSource = TeacherManager.GetAllTeacher();
            selTeacher2.DataTextField = "teacherName";
            selTeacher2.DataValueField = "teacherID";
            selTeacher2.DataBind();
        }
        public String ConventTeaIdToName(String teaID)
        {
            return BLL.TeacherManager.GetNameByID(teaID);
        }
        public String ConventEnumToString(String strClassTimeCode)
        {
            //OrderClassTimeSpan classTimeSpan = (OrderClassTimeSpan)Int32.Parse(strClassTimeCode);
            OrderClassTimeSpan classTimeSpan = (OrderClassTimeSpan)Enum.Parse(typeof(OrderClassTimeSpan), strClassTimeCode);
            String result = null;
            switch (classTimeSpan)
            {
                case OrderClassTimeSpan.labStatus12:
                    result = "上午一二节";
                    break;
                case OrderClassTimeSpan.labStatus34:
                    result = "上午三四节";
                    break;
                case OrderClassTimeSpan.labStatus56:
                    result = "下午一二节";
                    break;
                case OrderClassTimeSpan.labStatus78:
                    result = "下午三四节";
                    break;
                case OrderClassTimeSpan.labStatus910:
                    result = "晚上一二节";
                    break;
                default:
                    break;
            }
            return result;
        }
        private void bindLabIDs()
        {
            selLabs.DataSource = BLL.LabManager.GetLabIDs().OrderBy(ob => ob.Length);
            selLabs.DataBind();
            selLabs2.DataSource = BLL.LabManager.GetLabIDs().OrderBy(ob => ob.Length);
            selLabs2.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

        }

        protected void rpOrderLabManager_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {

        }

        protected void btnSingleContonation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(myHiddenField.Value))
            {
                //默认按照实验日期查询
                rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(DateTime.Parse(txtDatePicker.Text.Trim()));
                rpOrderLabManager.DataBind();
            }
            else
            {
                // 实验日期、实验室、教师、上课时间段、组合搜索
                switch (myHiddenField.Value)
                {
                    case "1":
                        rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(DateTime.Parse(txtDatePicker.Text.Trim()));
                        break;
                    case "2":
                        rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(DateTime.Now, selLabs.Value, null, Model.OrderClassTimeSpan.None, null);
                        break;
                    case "3":
                        rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(DateTime.Now, null, selTeacher.Value, Model.OrderClassTimeSpan.None, null);
                        break;
                    case "4":
                        Model.OrderClassTimeSpan orderClass = (OrderClassTimeSpan)Int32.Parse(selLabStatus.Value);
                        rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(DateTime.Now, null, selTeacher.Value, orderClass, null);
                        break;
                    case "5":
                        btnFullTextSearch_Click(this, e);
                        break;
                    default:
                        break;
                }
                rpOrderLabManager.DataBind();
            }
        }

        protected void btnFlushAdmin_Click(object sender, EventArgs e)
        {
            rpOrderLabManager.DataSource = LabOrderManager.GetAllOrder();
            rpOrderLabManager.DataBind();

        }

        protected void btnFullTextSearch_Click(object sender, EventArgs e)
        {
            //TODO: 1、获取布置的组合标签 tags   --->预约实验日期、实验室、预约上课时间段、教师ID
            List<String> tagKeys = new List<string> { };
            if (!String.IsNullOrWhiteSpace(txtDatePicker2.Text.Trim()))
            {
                tagKeys.Add(txtDatePicker2.Text.Trim());
            }
            tagKeys.Add(selLabs2.Value);
            tagKeys.Add(selLabStatus2.Value);
            tagKeys.Add(selTeacher2.Value);


            //TODO: 2、调用后台系统方法，并从新绑定数据
            var orderlist = BLL.LabOrderManager.GetAllOrder();
            System.Data.DataTable dtOrder = BLL.ConventerCommon.LinqToDataTable<Model.OrderLabs>(orderlist);

            String[] keywords = new string[] { selTeacher2.Value, selLabs2.Value, selTeacher2.Value };
            System.Data.DataView rowOrder = BLL.ConventerCommon.FullTextSearch(dtOrder, keywords);
            if (rowOrder.Count < 1)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('没有相关预约数据！')", true);
                List<OrderLabs> orderTmp = new List<OrderLabs>();
                rpOrderLabManager.DataSource = orderTmp;
                rpOrderLabManager.DataBind();
                return;
            }
            else
            {
                rpOrderLabManager.DataSource = rowOrder;
                rpOrderLabManager.DataBind();

            }

        }


        protected void btnlog4netDemo_Click(object sender, EventArgs e)
        {
            //创建日志记录组件实例
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //记录错误日志
            log.Error("error", new Exception("发生了一个异常！"));

            //记录严重错误
            log.Fatal("fatal", new Exception("发生了致命的错误！" + "\r\n"));

            //记录一般的信息
            log.Info("info", new Exception("一般的信息"));

            //记录调试信息
            log.Debug("debug");

            //记录警告信息
            log.Warn("warn");

            Response.Write("日志记录完毕。\n");
        }

    }
}